package com.sdy.auth.api.model;

import com.sdy.common.utils.MapUtil;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@Accessors(chain = true)
public class AuthDto {
    private Integer id;
    private Integer parentId;
    /**
     * 1-系统 2-菜单 3-请求
     */
    private Integer type;
    private String name;
    private String code;
    private Boolean auth;
    private String route;
    private List<AuthDto> children;
    
    public List<AuthDto> convertToList() {
        List<AuthDto> result = new ArrayList<>();
        result.add(this);
        if (children != null) {
            children.forEach(item -> result.addAll(item.convertToList()));
            children = null;
        }
        return result;
    }

    public Map<String, Boolean> convertToMap() {
        List<AuthDto> l = convertToList();
        Map<String, Boolean> m = new HashMap<>(l.size() + 10);
        l.forEach(item -> m.put(item.getCode(), item.getAuth()));
        return m;
    }
}
